package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.BmsBatch;
import com.tbit.main.pojo.BmsBattery;
import com.tbit.main.service.AccountService;
import com.tbit.main.service.BmsBatchService;
import com.tbit.main.service.BmsBatteryService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 云bms批次
 *
 * @author LMY
 * @create 2022-07-13 17:41
 */
@RestController
@RequestMapping("/bmsBatch")
public class BmsBatchController {

    @Autowired
    private BmsBatchService bmsBatchService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private BmsBatteryService bmsBatteryService;
    @Autowired
    private AccountService accountService;

    /**
     * 添加批次
     *
     * @param token
     * @param batchName 批次名称
     * @param batchTime 批次时间
     * @return null
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.BMSBATCH, methods = MethodEnum.BMSBATCH_ADD)
    public JsonResult add(@NotNull String token, @NotNull String batchName, @NotNull String batchTime) {

        Assert.hasLength(batchName, "ParkPoint.name.notNull");
        Assert.hasLength(batchTime, "AccountEvent.control.accountEventTime.notNull");

        AccountUser accountUser = tokenService.getAccount(token);

        Integer accountId = accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId();

        BmsBatch bmsBatchDB = bmsBatchService.getAccountIdBatchName(accountId, batchName);
        if (bmsBatchDB != null) {
            throw new BaseException("batch.ter.exit");
        } else {
            BmsBatch bmsBatch = new BmsBatch();
            bmsBatch.setAccountId(accountId);
            bmsBatch.setBatchName(batchName);
            bmsBatch.setBatchTime(batchTime);
            bmsBatch.setAddTime(DateTimeUtil.getNowTime());
            bmsBatchService.insert(bmsBatch);
        }
        return JsonResult.succ();
    }

    /**
     * 根据品牌查询所有
     *
     * @param token
     * @return
     */
    @RequestMapping("/getAccountId")
    public JsonResult<List<BmsBatch>> getAccountId(@NotNull String token) {

        AccountUser accountUser = tokenService.getAccount(token);

        List<BmsBatch> bmsBatchList = bmsBatchService.getAccountId(accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId());

        return JsonResult.succ(bmsBatchList);
    }

    /**
     * 删除批次
     *
     * @param token
     * @param batch 批次号
     * @return null
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.BMSBATCH, methods = MethodEnum.BMSBATCH_DEL)
    public JsonResult del(@NotNull String token, @NotNull Integer batch) {

        List<BmsBattery> bmsBatteries = bmsBatteryService.getBatch(batch);
        if (bmsBatteries != null && bmsBatteries.size() > 0) {
            throw new BaseException("batch.battery.exit");
        }

        bmsBatchService.del(batch);

        return JsonResult.succ();
    }

    /**
     * 修改批次
     *
     * @param token
     * @param batch     批次号
     * @param batchName 批次名称
     * @param batchTime 批次日期
     * @return
     */
    @RequestMapping("/update")
    @OperateLog(module = ModuleEnum.BMSBATCH, methods = MethodEnum.BMSBATCH_UPDATE)
    public JsonResult update(@NotNull String token, @NotNull Integer batch, @NotNull String batchName, @NotNull String batchTime) {

        BmsBatch bmsBatch = new BmsBatch();
        bmsBatch.setBatch(batch);
        bmsBatch.setBatchName(batchName);
        bmsBatch.setBatchTime(batchTime);
        bmsBatchService.update(bmsBatch);

        return JsonResult.succ();
    }


}
